package com.gcp.freemarker.controller;

import com.gcp.freemarker.service.TableService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/order")
public class TableController {

    @Resource
    private TableService tableService;
    
    @Value("${code.real-path}")
    private String realPath;
    
    @Value("${code.package-name}")
    private String packageName;

    @GetMapping("/generateCode")
    public String generateCode(String tableName,String tableComment) {
        //生成m_order表相关的源代码，并保存在本项目的包内
        return tableService.generateCode(tableName, realPath, snakeToPascalCase(tableName), packageName, tableComment);
    }

    public static String snakeToPascalCase(String snakeCase) {
        if (snakeCase == null || snakeCase.isEmpty()) {
            return snakeCase;
        }

        StringBuilder result = new StringBuilder();
        String[] words = snakeCase.split("_");

        for (String word : words) {
            if (!word.isEmpty()) {
                result.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    result.append(word.substring(1).toLowerCase());
                }
            }
        }

        return result.toString();
    }

}
